<?php

App::uses('AppController', 'Controller');
App::uses('Paypal', 'Paypal.Lib');
/**

 * Properties Controller

 *

 * @property Property $Property

 * @property PaginatorComponent $Paginator

 * @property SessionComponent $Session

 */

class PropertiesController extends AppController {



/**

 * Components

 *

 * @var array

 */

	public $components = array('Paginator', 'Session');







	    public function beforeFilter(){ 

		

			//$this->Security->unlockedFields = array('Property');



// 			$this->Aurh->allow(array('*'));

        	$this->Auth->allow('index','view','side_bar','side_bar_property_seatch');

			parent::beforeFilter();

		}

/**

 * index method

 *

 * @return void

 */
	public function Paypal() {
		$this->Paypal = new Paypal(array(
    'sandboxMode' => true,
    'nvpUsername' => 'taimur911_api1.gmail.com',
    'nvpPassword' => '7JVWFTLC87LLMVNM',
    'nvpSignature' => 'Ai1PaghZh5FmBLCDCTQpwG8jB264Ar4gnYNdMq2vciWnZ1UYaiBtxgrK'
));


$order = array(
    'description' => 'Your purchase with Acme clothes store',
    'currency' => 'GBP',
    'return' => 'http://localhost/property/property-ali/Properties/Paypal',
    'cancel' => 'http://localhost/property/property-ali/Properties/Paypal',
    'custom' => 'bingbong',
    'shipping' => '1.50',
    'items' => array(
        0 => array(
            'name' => 'Package 1',
            'description' => '10 Properties, 1 features',
            'subtotal' => 99.00,
            'qty' => 2,
        ),
    )
);
 try {
   $responses	=	 $this->Paypal->setExpressCheckout($order);
} catch (Exception $e) {
    // $e->getMessage();
}    

//$responses[]
try {
  $arra	=	 $this->Paypal->getExpressCheckoutDetails($responses['TOKEN']);
 //  $arra	=	 $this->Paypal->getExpressCheckoutDetails('EC-06281552LR8169538');
} catch (Exception $e) {
     $e->getMessage();
}  

try {
//   $arra	=	 $this->Paypal->getExpressCheckoutDetails($responses['TOKEN']);
   $doExpressCheckoutPayment	=	 $this->Paypal->doExpressCheckoutPayment($order, 'EC-06281552LR8169538' , 'TS7VHNMCHC3LN');
} catch (Exception $e) {
     $e->getMessage();
}  



pr($arra);
echo '<hr/>';
pr($responses);
echo '<hr/>';
pr($doExpressCheckoutPayment);
	}

	public function index() {
	
	
		$pt = $ct =	$ci	= $mnb = $mxb =	$mnp = $mxp	= $mna =$mxa = '';

		if(isset($this->request->query['pt']) and !empty($this->request->query['pt']))
		$pt	=	(int)trim($this->request->query['pt']);//Property Type ID

		if(isset($this->request->query['ct']) and !empty($this->request->query['ct']))
		$ct	=	trim($this->request->query['ct']);//Contrat Type

		if(isset($this->request->query['ci']) and !empty($this->request->query['ci']))
		$ci	=	(int)trim($this->request->query['ci']);//City Id

		if(isset($this->request->query['mnb']) and !empty($this->request->query['mnb']))
		$mnb	=	(int)trim($this->request->query['mnb']);//Minumum beds

		if(isset($this->request->query['mnbt']) and !empty($this->request->query['mnbt']))
		$mnbt	=	(int)trim($this->request->query['mnbt']);//Minumum BATH

		if(isset($this->request->query['mnp']) and !empty($this->request->query['mnp']))
		$mnp	=	(int)trim($this->request->query['mnp']);//Minumum price

		if(isset($this->request->query['mxp']) and !empty($this->request->query['mxp']))
		$mxp	=	(int)trim($this->request->query['mxp']);//Maximum price

		if(isset($this->request->query['mna']) and !empty($this->request->query['mna']))
		$mna	=	(int)trim($this->request->query['mna']);//Minumum Area

		if(isset($this->request->query['mxa']) and !empty($this->request->query['mxa']))
		$mxa	=	(int)trim($this->request->query['mxa']);//Maximum price

		$search=$search_pt=$search_ct=$search_ci=$search_mnb=$search_mnbt=$search_mnp = $search_mxp= $search_mna = $search_mxa='';
		
		
		$search	=	array('AND'=>array('User.active' => 1,'User.expiry_date >= ' => date('Y-m-d H:i'),'User.approved'=>1,'Property.active' => 1));
		
		if(isset($pt) and !empty($pt))
		$search_pt	=	array('AND'=>array('Property.property_type_id'=>$pt));
		
		if(isset($ct) and !empty($ct))
		$search_ct	=	array('AND'=>array('Property.contract_type'=>$ct));
		
		if(isset($ci) and !empty($ci))
		$search_ci	=	array('AND'=>array('Property.city_id'=>$ci));

		if(isset($mnb) and !empty($mnb))
		$search_mnb	=	array('AND'=>array('Property.beds >='=>$mnb));

		if(isset($mnbt) and !empty($mnbt))
		$search_mnbt	=	array('AND'=>array('Property.bath >='=>$mnbt));


		if(isset($mnp) and !empty($mnp))
		$search_mnp	=	array('AND'=>array('Property.price >='=>$mnp));

		if(isset($mxp) and !empty($mxp))
		$search_mxp	=	array('AND'=>array('Property.price <='=>$mxp));

		if(isset($mna) and !empty($mna))
		$search_mna	=	array('AND'=>array('Property.area >='=>$mna));

		if(isset($mxa) and !empty($mxa))
		$search_mxa	=	array('AND'=>array('Property.area <='=>$mxa));


		
		$options	=	array($search,$search_pt,$search_ct,$search_ci,$search_mnb,$search_mnp,$search_mxp,$search_mna,$search_mxa);

		$this->Property->belongs_to();

/*$this->Property->virtualFields = array(
    'ttl' => 'Property.title',
    'prs' => 'Property.price',
    'bds' => 'Property.beds',
    'bat' => 'Property.baths',
    'prk' => 'Property.parking',
    'ara' => 'Property.area',
);*/
		$this->paginate = array('paramType' => 'querystring','limit' => 12,'order'=>'Property.created desc');
		$this->Property->recursive = 0;

		$this->set('properties', $this->Paginator->paginate($options));

	}

	

	public function my_properties() {

		$user_id	=	$this->Auth->user('id');

 		$user_role	=	$this->Auth->user('user_role');
		if(($user_role=='broker_agent'))
		$options = array('AND' => array('Property.created_by' => $user_id));
		else
		$options = array('AND' => array('Property.user_id' => $user_id));

		$this->paginate = array('limit' => 20,'order'=>'Property.id desc');
		$this->Property->recursive = 0;
		$this->set('properties', $this->Paginator->paginate($options));

	}

/**

 * view method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */	

 	public function side_bar() {

		$this->LoadModel('User');

		$options = array('recursive' => 0,'limit'=>6,'order'=>'RAND()'
		,'conditions' => array('User.expiry_date >= ' => date('Y-m-d H:i'),'User.active' => 1,'User.approved'=>1,'Property.active' => 1));

		$result['Property']	=	$this->Property->find('all', $options);


		$options = array('recursive' => -1,'limit'=>4,'order'=>'RAND()','conditions' => array('User.expiry_date >= ' => date('Y-m-d H:i'),'User.active' => 1,'User.approved'=>1,'User.user_role'=>array('agent','broker_admin')));
		$result['User']	=	$this->User->find('all', $options);


			$this->LoadModel('PropertyType');
			$this->LoadModel('City');
			$result['getPropertyType']	= 	$this->PropertyType->getPropertyType();
			$result['getCity']	= 	$this->City->getCity();


		return $result;

	}

 	public function side_bar_property_seatch() {

			$this->LoadModel('PropertyType');
			$this->LoadModel('City');
			$result['getPropertyType']	= 	$this->PropertyType->getPropertyType();
			$result['getCity']	= 	$this->City->getCity();


		return $result;

	}

	

	

	public function view($id = null) {

		if (!$this->Property->exists($id)) {
			throw new NotFoundException(__('Invalid property'));
		}


		$options = array('conditions' => array('User.expiry_date >= ' => date('Y-m-d H:i'),'Property.active'=>1,'Property.' . $this->Property->primaryKey => $id));
		 $property	=	$this->Property->find('first', $options);
		 
		if (!$property)
			throw new NotFoundException(__('Invalid property'));
		 
		$this->set('property', $property);
		$options = array('recursive' => -1,'limit'=>3,'order'=>'RAND()','conditions' => array('Property.active'=>1,'Property.id !=' => $id));
		$properties	=	$this->Property->find('all', $options);
		$this->set('properties',$properties);

	}



/**

 * add method

 *

 * @return void

 */

	public function add() {

		if ($this->request->is('post')) {

					$user_id	=	$this->Auth->user('id');

 		$user_role	=	$this->Auth->user('user_role');
		if(($user_role=='broker_agent'))
			$user_id	=	$this->Auth->user('broker_id');


			$this->request->data['Property']['user_id']	=	$user_id;

			$this->Property->create();

			if ($this->Property->save($this->request->data)) {

				$this->Session->setFlash(__('The property has been saved.'), 'default', array('class' => 'alert alert-success'));

				return $this->redirect(array('action' => 'my_properties'));

			} else {

				$this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'error');

			}

		}

		$cities = $this->Property->City->find('list');

		$propertyTypes = $this->Property->PropertyType->find('list');

		$this->set(compact('cities', 'propertyTypes'));

	}



/**

 * edit method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */

	public function edit($id = null) {

		if (!$this->Property->exists($id)) {
			throw new NotFoundException(__('Invalid property'));
		}
		$user_id	=	$this->Auth->user('id');
		///////////////////////////////////////////////////////////////
 		$user_role	=	$this->Auth->user('user_role');
		///////////////////////////////////////////////////////////////

		
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Property->save($this->request->data)) {
				$this->Session->setFlash(__('The property has been saved.'), 'default', array('class' => 'alert alert-success'));
				return $this->redirect(array('action' => 'my_properties'));
				//return $this->redirect($this->referer());
				
			} else {
				$this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'error', array('class' => 'alert alert-danger'));
			}
		$options = array('recursive' => -1,'conditions' => array('Property.' . $this->Property->primaryKey => $id));
		$property = $this->Property->find('first', $options);
			
		} else {

		if(($user_role=='broker_agent'))
			$options = array('recursive' => -1,'conditions' => array('Property.created_by' => $user_id,'Property.' . $this->Property->primaryKey => $id));
			else
			$options = array('recursive' => -1,'conditions' => array('Property.user_id' => $user_id,'Property.' . $this->Property->primaryKey => $id));


			$property	=	$this->Property->find('first', $options);
			if($property){
			$this->request->data = $property;}
			else {
				$this->Session->setFlash(__('Access Denied.'), 'error', array('class' => 'alert alert-danger'));
				return $this->redirect(array('action' => 'my_properties'));
			}
			
		}

		
	//	$users = $this->Property->User->find('list');
		$cities = $this->Property->City->find('list');
		$propertyTypes = $this->Property->PropertyType->find('list');
		$this->set(compact('users', 'cities', 'propertyTypes','property'));

	}



/**

 * delete method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */

	public function delete($id = null) {

		$this->Property->id = $id;

		if (!$this->Property->exists()) {

			throw new NotFoundException(__('Invalid property'));

		}

		$this->request->onlyAllow('post', 'delete');

		if ($this->Property->delete()) {

			$this->Session->setFlash(__('The property has been deleted.'), 'default', array('class' => 'alert alert-success'));

		} else {

			$this->Session->setFlash(__('The property could not be deleted. Please, try again.'), 'default', array('class' => 'alert alert-danger'));

		}

		return $this->redirect(array('action' => 'index'));

	}



/**

 * admin_index method

 *

 * @return void

 */

	public function admin_index() {
		$this->paginate = array('order'=>'Property.id desc');
		$this->Property->recursive = 0;

		$this->set('properties', $this->Paginator->paginate());

	}



/**

 * admin_view method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */

	public function admin_view($id = null) {

		if (!$this->Property->exists($id)) {

			throw new NotFoundException(__('Invalid property'));

		}

		$options = array('conditions' => array('Property.' . $this->Property->primaryKey => $id));

		$this->set('property', $this->Property->find('first', $options));

	}



/**

 * admin_add method

 *

 * @return void

 */

	public function admin_add() {

		if ($this->request->is('post')) {

			$this->Property->create();

			if ($this->Property->save($this->request->data)) {

				$this->Session->setFlash(__('The property has been saved.'), 'default', array('class' => 'alert alert-success'));

				return $this->redirect(array('action' => 'index'));

			} else {

				$this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));

			}

		}

		$users = $this->Property->User->find('list',array('conditions'=>array('User.user_role'=>array('agent','broker_admin'))));

		$cities = $this->Property->City->find('list');

		$propertyTypes = $this->Property->PropertyType->find('list');

		$this->set(compact('users', 'cities', 'propertyTypes'));

	}



/**

 * admin_edit method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */

	public function admin_edit($id = null) {

		if (!$this->Property->exists($id)) {

			throw new NotFoundException(__('Invalid property'));

		}

		if ($this->request->is(array('post', 'put'))) {

			if ($this->Property->save($this->request->data)) {

				$this->Session->setFlash(__('The property has been saved.'), 'default', array('class' => 'alert alert-success'));

				return $this->redirect(array('action' => 'index'));

			} else {

				$this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));

			}
			$options = array('conditions' => array('Property.' . $this->Property->primaryKey => $id));
			$this->request->data = $property	=	 $this->Property->find('first', $options);

		} else {

			$options = array('conditions' => array('Property.' . $this->Property->primaryKey => $id));
			$this->request->data = $property	=	 $this->Property->find('first', $options);

		}

		$users = $this->Property->User->find('list',array('conditions'=>array('User.user_role'=>array('agent','broker_admin'))));

		$cities = $this->Property->City->find('list');

		$propertyTypes = $this->Property->PropertyType->find('list');

		$this->set(compact('users', 'cities', 'propertyTypes','property'));

	}



/**

 * admin_delete method

 *

 * @throws NotFoundException

 * @param string $id

 * @return void

 */

	public function admin_delete($id = null) {

		$this->Property->id = $id;

		if (!$this->Property->exists()) {

			throw new NotFoundException(__('Invalid property'));

		}

		$this->request->onlyAllow('post', 'delete');

		if ($this->Property->delete()) {

			$this->Session->setFlash(__('The property has been deleted.'), 'default', array('class' => 'alert alert-success'));

		} else {

			$this->Session->setFlash(__('The property could not be deleted. Please, try again.'), 'default', array('class' => 'alert alert-danger'));

		}

		return $this->redirect(array('action' => 'index'));

	}

}

